package com.olocat.cjx.nsims.controller

import com.alibaba.fastjson.JSON
import com.olocat.cjx.nsims.pojo.entry.EntryNurse
import com.olocat.cjx.nsims.pojo.personnel.Nurse
import com.olocat.cjx.nsims.dao.NurseRepository
import com.olocat.cjx.nsims.dao.EntryNurseRepository
import com.olocat.cjx.nsims.dao.SchedulingRepository
import com.olocat.cjx.nsims.dao.UserRepository
import com.olocat.cjx.nsims.pojo.info.Scheduling
import com.olocat.cjx.nsims.pojo.personnel.User
import com.olocat.cjx.nsims.service.NurseService
import com.olocat.cjx.nsims.service.UserService
import com.olocat.cjx.nsims.util.Response
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource

@RestController
class NurseController {

    @Resource
    lateinit var nurseService: NurseService
    @Resource
    lateinit var nurseRepository: NurseRepository
    @Resource
    lateinit var entryNurseRepository: EntryNurseRepository
    @Resource
    lateinit var schedulingRepository: SchedulingRepository
    @Resource
    lateinit var userService: UserService
    val log = LoggerFactory.getLogger(NurseController::class.java)!!

    @GetMapping("/api/nurse")
    fun get(id: String?, department: Int?): Response {
        return if (id == null) {
            if (department == null)
            Response.builder(nurseService.list())
            else Response.builder(nurseService.getNursesByDepartment(department))
        } else {
            Response.builder(nurseService.getById(id))
        }
    }
    @DeleteMapping("/api/nurse/del")
    fun del(id: String): Response {
        println("/api/nurse?id=$id")
        nurseService.removeById(id)
        return Response.builder()
    }

    @PutMapping("/api/nurse/update")
    fun update(@RequestBody json: String): Response {
        val nurse = JSON.parseObject(json, Nurse::class.java)
        nurseService.updateById(nurse)
        return Response.builder()
    }

    @PostMapping("/api/nurse/entry")
    fun add(@RequestBody json: String?): Response {
        log.info("添加护士信息 json: $json")
        val nurse = JSON.parseObject(json, Nurse::class.java)
        println(nurse)
        nurseRepository.save(nurse)
        val user = User()
        user.name = nurse.name
        user.telephone = nurse.phone
        userService.saveNurseUser(user)
        return Response.builder()
    }

    @GetMapping("/api/nurse/entry/get")
    fun getEntry(id: String?): Response {
        return if (id == null) {
            log.info("获取入职护士列表")
            Response.builder(entryNurseRepository.findAll())
        } else {
            Response.builder(entryNurseRepository.getOne(id))
        }

    }

    @GetMapping("/api/nurse/query")
    fun getByName(name: String?): Response {
        log.info("姓名查询 $name")
        return if (name == null) {
            log.info("name is null")
            return Response.builder()
        } else {
            Response.builder(nurseRepository.getByName(name))
        }
    }

    @PostMapping("/api/scheduling")
    fun updateScheduling(@RequestBody json: String): Response {
        println(json)
        val scheduling = JSON.parseObject(json, Scheduling::class.java)
        schedulingRepository.save(scheduling)
        return Response.builder()
    }
    @GetMapping("/api/nurse/department")
    fun getNurseByDepartment(department: Int): Response {
        val nurse = nurseService.getNursesByDepartment(department);
        println("nurse" + nurse)
        return Response.builder(nurse)
    }
}